﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RoboticOperationMerge.CUiROMRoboticOperationGridWrapper
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using C1.Win.C1FlexGrid.Util.BaseControls;
using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui48;


namespace DnProcessSimulateCommands.RoboticOperationMerge;

internal class CUiROMRoboticOperationGridWrapper : TxFlexGrid
{
  private int m_lastSelectedRow = -1;
  private int m_lastSelectedColumn = -1;
  private int m_lastSelectedTreeRow = -1;
  private bool m_treeSelectionChanged;
  private bool m_entireRowSelection = true;
  private List<int> m_treeSelection = new List<int>(0);
  private CUiROMDataUtils.ManualSelection m_manualSelection = CUiROMDataUtils.ManualSelection.CtrlShift;
  private Dictionary<string, string> m_columnCustomSelectionStyles = new Dictionary<string, string>();
  private IContainer components;
  private ImageList m_notificationImages;
  private Dictionary<string, string> m_columnCustomNormalStyles = new Dictionary<string, string>();
  private int m_treeColumnAutosizedWidth = -1;
  private double m_linearMultiplier = 1.0;
  private int m_linearDigitsAfterDecimalPoint = 2;

  public event TreeSelectionChangedEventHandler TreeSelectionChanged;

  public CUiROMRoboticOperationGridWrapper()
  {
    this.InitializeComponent();
    this.RegisterForEvents();
  }

  public List<int> SelectedTreeRows => this.m_treeSelection;

  public bool EntireRowSelection => this.m_entireRowSelection;

  public CUiROMDataUtils.ManualSelection ManualSelection
  {
    get => this.m_manualSelection;
    set => this.m_manualSelection = value;
  }

  public double LinearMultiplier
  {
    get => this.m_linearMultiplier;
    set => this.m_linearMultiplier = value;
  }

  public int LinearDigitsAfterDecimalPoint
  {
    get => this.m_linearDigitsAfterDecimalPoint;
    set => this.m_linearDigitsAfterDecimalPoint = value;
  }

  private void InitializeComponent()
  {
    this.components = (IContainer) new Container();
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiROMRoboticOperationGridWrapper));
    this.m_notificationImages = new ImageList(this.components);
    ((ISupportInitialize) this).BeginInit();
    ((Control) this).SuspendLayout();
    this.m_notificationImages = new ImageList();
    this.m_notificationImages.TransparentColor = Color.Transparent;
    this.m_notificationImages.ColorDepth = ColorDepth.Depth32Bit;
    this.m_notificationImages.Images.Add(TxImageManager.GetImageByKey("SuccessfulGreen").ConvertTo<Image>(new Size()));
    ((C1FlexGridBase) this).DrawMode = (DrawModeEnum) 1;
    ((C1FlexGridBase) this).SelectionMode = (SelectionModeEnum) 1;
    ((ISupportInitialize) this).EndInit();
    ((Control) this).ResumeLayout(false);
  }

  protected virtual void SetupCustomStyles()
  {
    base.SetupCustomStyles();
    CellStyle normal = ((C1FlexGridBase) this).Styles.Normal;
    CellStyle cellStyle1 = ((C1FlexGridBase) this).Styles.Add("matchColumnNormalStyle");
    cellStyle1.ForeColor = CUiROMDataUtils.FORECOLOR_MATCHED_LOCATION;
    cellStyle1.TextAlign = (TextAlignEnum) 1;
    ((C1FlexGridBase) this).Styles.Add("matchColumnSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]).TextAlign = (TextAlignEnum) 1;
    ((C1FlexGridBase) this).Styles.Add("distanceColumnNormalStyle").TextAlign = (TextAlignEnum) 1;
    ((C1FlexGridBase) this).Styles.Add("distanceColumnSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]).TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle2 = ((C1FlexGridBase) this).Styles.Add("mergeColumnNormalStyle");
    cellStyle2.ImageAlign = (ImageAlignEnum) 4;
    cellStyle2.TextAlign = (TextAlignEnum) 1;
    ((C1FlexGridBase) this).Styles.Add("mergeColumnSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]).TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle3 = ((C1FlexGridBase) this).Styles.Add("targetOperationNormalStyle");
    cellStyle3.ForeColor = CUiROMDataUtils.FORECOLOR_TARGET_OPERATION;
    cellStyle3.TextAlign = (TextAlignEnum) 1;
    ((C1FlexGridBase) this).Styles.Add("sourceOperationSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]).TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle4 = ((C1FlexGridBase) this).Styles.Add("targetLocationNormalStyle");
    cellStyle4.ForeColor = CUiROMDataUtils.FORECOLOR_TARGET_LOCATION;
    cellStyle4.TextAlign = (TextAlignEnum) 1;
    ((C1FlexGridBase) this).Styles.Add("targetLocationSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]).TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle5 = ((C1FlexGridBase) this).Styles.Add("targetGrayedLocationNormalStyle");
    cellStyle5.ForeColor = CUiROMDataUtils.FORECOLOR_TARGET_GRAYED_LOCATION;
    cellStyle5.Font = cellStyle5.Font = new Font(((Control) this).Font, FontStyle.Italic);
    cellStyle5.TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle6 = ((C1FlexGridBase) this).Styles.Add("targetGrayedLocationSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]);
    cellStyle6.Font = new Font(((Control) this).Font, FontStyle.Italic);
    cellStyle6.TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle7 = ((C1FlexGridBase) this).Styles.Add("sourceOperationNormalStyle");
    cellStyle7.ForeColor = CUiROMDataUtils.FORECOLOR_SOURCE_OPERATION;
    cellStyle7.TextAlign = (TextAlignEnum) 1;
    ((C1FlexGridBase) this).Styles.Add("sourceOperationSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]).TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle8 = ((C1FlexGridBase) this).Styles.Add("sourceLocationNormalStyle");
    cellStyle8.ForeColor = CUiROMDataUtils.FORECOLOR_SOURCE_LOCATION;
    cellStyle8.TextAlign = (TextAlignEnum) 1;
    ((C1FlexGridBase) this).Styles.Add("sourceLocationSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]).TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle9 = ((C1FlexGridBase) this).Styles.Add("sourceGrayedLocationNormalStyle");
    cellStyle9.ForeColor = CUiROMDataUtils.FORECOLOR_SOURCE_GRAYED_LOCATION;
    cellStyle9.Font = cellStyle9.Font = new Font(((Control) this).Font, FontStyle.Italic);
    cellStyle9.TextAlign = (TextAlignEnum) 1;
    CellStyle cellStyle10 = ((C1FlexGridBase) this).Styles.Add("sourceGrayedLocationSelectStyle", ((C1FlexGridBase) this).Styles["Selected"]);
    cellStyle10.Font = cellStyle10.Font = new Font(((Control) this).Font, FontStyle.Italic);
    cellStyle10.TextAlign = (TextAlignEnum) 1;
  }

  public void RegisterForEvents()
  {
    ((Control) this).MouseDown += new MouseEventHandler(this.CUiROMRoboticOperationGridWrapper_MouseDown);
    ((C1FlexGridBase) this).BeforeSelChange += new RangeEventHandler(this.CUiROMRoboticOperationGridWrapper_BeforeSelChange);
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiROMRoboticOperationGridWrapper_OwnerDrawCell);
    ((C1FlexGridBase) this).BeforeEdit += new RowColEventHandler(this.CUiROMRoboticOperationGridWrapper_BeforeEdit);
  }

  public void UnregisterForEvents()
  {
    ((Control) this).MouseDown -= new MouseEventHandler(this.CUiROMRoboticOperationGridWrapper_MouseDown);
    ((C1FlexGridBase) this).BeforeSelChange -= new RangeEventHandler(this.CUiROMRoboticOperationGridWrapper_BeforeSelChange);
    ((C1FlexGridBase) this).OwnerDrawCell -= new OwnerDrawCellEventHandler(this.CUiROMRoboticOperationGridWrapper_OwnerDrawCell);
    ((C1FlexGridBase) this).BeforeEdit -= new RowColEventHandler(this.CUiROMRoboticOperationGridWrapper_BeforeEdit);
  }

  public void InitGrid(ArrayList nodeInfoList)
  {
    ((ScrollableControl) this).BeginUpdate();
    int count = ((RowColCollection) ((C1FlexGridBase) this).Rows).Count;
    int num = ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed;
    ((C1FlexGridBase) this).Rows.RemoveRange(num, count - num);
    this.m_treeSelection.Clear();
    this.InsertRows(1, nodeInfoList, false);
    ((ScrollableControl) this).EndUpdate();
  }

  public void AddColumnCustomNormalStyle(string colName, string styleName)
  {
    if (colName == null || styleName == null)
      return;
    if (this.m_columnCustomNormalStyles.ContainsKey(colName))
      this.m_columnCustomNormalStyles.Remove(colName);
    this.m_columnCustomNormalStyles.Add(colName, styleName);
  }

  public void AddColumnCustomSelectionStyle(string colName, string styleName)
  {
    if (colName == null || styleName == null)
      return;
    if (this.m_columnCustomSelectionStyles.ContainsKey(colName))
      this.m_columnCustomSelectionStyles.Remove(colName);
    this.m_columnCustomSelectionStyles.Add(colName, styleName);
  }

  public CellStyle GetColumnCustomSelectionStyle(int column)
  {
    CellStyle customSelectionStyle1 = (CellStyle) null;
    string name = ((C1FlexGridBase) this).Cols[column].Name;
    if (name != null && this.m_columnCustomSelectionStyles.ContainsKey(name))
    {
      string customSelectionStyle2 = this.m_columnCustomSelectionStyles[name];
      if (customSelectionStyle2 != null && ((C1FlexGridBase) this).Styles.Contains(customSelectionStyle2))
        customSelectionStyle1 = ((C1FlexGridBase) this).Styles[customSelectionStyle2];
    }
    return customSelectionStyle1;
  }

  public CellStyle GetSpecificDataStyle(
    int row,
    int column,
    CUiROMDataUtils.CellStyleCategory styleCategory)
  {
    CellStyle specificDataStyle = (CellStyle) null;
    if (row > 0 && row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count && column >= 0 && column < ((RowColCollection) ((C1FlexGridBase) this).Cols).Count)
    {
      Node node = ((C1FlexGridBase) this).Rows[row].Node;
      if (node != null && node.Key is CUiROMGridNodeInfo key)
      {
        string name = ((C1FlexGridBase) this).Cols[column].Name;
        if (key is CUiROMTargetInfo cuiRomTargetInfo && !cuiRomTargetInfo.BelongToTarget && (name == "Match" || name == "Distance"))
        {
          switch (styleCategory)
          {
            case CUiROMDataUtils.CellStyleCategory.Normal:
              specificDataStyle = ((C1FlexGridBase) this).Styles["targetGrayedLocationNormalStyle"];
              break;
            case CUiROMDataUtils.CellStyleCategory.Select:
              specificDataStyle = ((C1FlexGridBase) this).Styles["targetGrayedLocationSelectStyle"];
              break;
          }
        }
      }
      if (specificDataStyle == null)
      {
        switch (styleCategory)
        {
          case CUiROMDataUtils.CellStyleCategory.Normal:
            specificDataStyle = this.GetColumnCustomNormalStyle(column);
            break;
          case CUiROMDataUtils.CellStyleCategory.Select:
            specificDataStyle = this.GetColumnCustomSelectionStyle(column);
            break;
          default:
            specificDataStyle = this.GetColumnCustomNormalStyle(column);
            break;
        }
      }
    }
    return specificDataStyle;
  }

  public CellStyle GetColumnCustomNormalStyle(int column)
  {
    CellStyle customNormalStyle1 = (CellStyle) null;
    string name = ((C1FlexGridBase) this).Cols[column].Name;
    if (name != null && this.m_columnCustomNormalStyles.ContainsKey(name))
    {
      string customNormalStyle2 = this.m_columnCustomNormalStyles[name];
      if (customNormalStyle2 != null && ((C1FlexGridBase) this).Styles.Contains(customNormalStyle2))
        customNormalStyle1 = ((C1FlexGridBase) this).Styles[customNormalStyle2];
    }
    return customNormalStyle1;
  }

  public bool SetCustomCellStyle(
    int row,
    int column,
    CUiROMDataUtils.CellStyleCategory styleCategory)
  {
    bool flag = false;
    if (row > 0 && row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count && column >= 0 && column < ((RowColCollection) ((C1FlexGridBase) this).Cols).Count)
    {
      CellStyle cellStyle = this.GetSpecificDataStyle(row, column, styleCategory) ?? ((C1FlexGridBase) this).Styles.Normal;
      ((C1FlexGridBase) this).SetCellStyle(row, column, cellStyle);
      flag = true;
    }
    return flag;
  }

  private void RefreshDataRows()
  {
    for (int index = 1; index < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count; ++index)
    {
      Node node = ((C1FlexGridBase) this).Rows[index].Node;
      if (node != null && node.Key is CUiROMGridNodeInfo key)
        key.GridRow = ((C1FlexGridBase) this).Rows[index];
    }
  }

  public void InsertRows(int rowIndex, ArrayList nodeInfoList, bool preserveTreeSelection)
  {
    if (nodeInfoList == null || nodeInfoList.Count <= 0 || rowIndex <= 0 || rowIndex > ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      return;
    this.m_treeSelectionChanged = false;
    if (!preserveTreeSelection && this.m_treeSelection.Count > 0)
      this.m_treeSelectionChanged = this.ClearTreeSelection();
    int count = nodeInfoList.Count;
    int num = rowIndex;
    for (int index = 0; index < count; ++index)
    {
      if (nodeInfoList[index] is CUiROMGridNodeInfo nodeInfo)
      {
        int level = nodeInfo.Level;
        Node node = ((C1FlexGridBase) this).Rows.InsertNode(num, level);
        if (node != null)
        {
          node.Key = (object) nodeInfo;
          num = ((RowCol) node.Row).Index + 1;
        }
      }
    }
    this.UpdateRows(rowIndex, num - rowIndex, true);
    if (preserveTreeSelection)
    {
      this.RefreshTreeSelectionList();
      this.m_treeSelectionChanged = true;
    }
    this.RefreshDataRows();
    if (!this.m_treeSelectionChanged)
      return;
    this.NotifyTreeSelectionChanged();
  }

  public bool RemoveRows(List<int> rows, bool preserveTreeSelection)
  {
    bool flag = false;
    if (rows != null)
    {
      if (rows.Count > 0)
      {
        try
        {
          this.m_treeSelectionChanged = false;
          Array array = (Array) rows.ToArray();
          Array.Sort(array);
          if (!preserveTreeSelection && this.m_treeSelection.Count > 0)
            this.m_treeSelectionChanged = this.ClearTreeSelection();
          for (int index = array.Length - 1; index >= 0; --index)
          {
            int num = (int) array.GetValue(index);
            if (num > 0 && num < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
              ((C1FlexGridBase) this).Rows.Remove(num);
          }
          this.RefreshDataRows();
          if (preserveTreeSelection)
          {
            this.RefreshTreeSelectionList();
            this.m_treeSelectionChanged = true;
          }
          if (this.m_treeSelectionChanged)
            this.NotifyTreeSelectionChanged();
          flag = true;
        }
        catch (Exception ex)
        {
          flag = false;
        }
      }
    }
    return flag;
  }

  public bool MoveRows(List<int> rows, int offset, bool preserveTreeSelection)
  {
    bool flag = false;
    if (rows != null && rows.Count > 0)
    {
      if (offset != 0)
      {
        try
        {
          this.m_treeSelectionChanged = false;
          Array array = (Array) rows.ToArray();
          Array.Sort(array);
          if (!preserveTreeSelection && this.m_treeSelection.Count > 0)
            this.m_treeSelectionChanged = this.ClearTreeSelection();
          if (offset > 0)
          {
            for (int index = array.Length - 1; index >= 0; --index)
            {
              int num1 = (int) array.GetValue(index);
              int num2 = num1 + offset;
              if (num1 > 0 && num1 < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count && num2 > 0 && num2 < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
                ((RowColCollection) ((C1FlexGridBase) this).Rows).Move(num1, num2);
            }
          }
          else if (offset < 0)
          {
            for (int index = 0; index < array.Length; ++index)
            {
              int num3 = (int) array.GetValue(index);
              int num4 = num3 + offset;
              if (num3 > 0 && num3 < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count && num4 > 0 && num4 < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
                ((RowColCollection) ((C1FlexGridBase) this).Rows).Move(num3, num4);
            }
          }
          this.RefreshDataRows();
          if (preserveTreeSelection)
          {
            this.RefreshTreeSelectionList();
            this.m_treeSelectionChanged = true;
          }
          if (this.m_treeSelectionChanged)
            this.NotifyTreeSelectionChanged();
          flag = true;
        }
        catch (Exception ex)
        {
          flag = false;
        }
      }
    }
    return flag;
  }

  public void UpdateRows(int rowIndex, int count, bool updateStyle)
  {
    if (count <= 0 || rowIndex <= 0 || rowIndex + count > ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      return;
    if (count > ((RowColCollection) ((C1FlexGridBase) this).Rows).Count - rowIndex)
      count = ((RowColCollection) ((C1FlexGridBase) this).Rows).Count - rowIndex;
    for (int row = rowIndex; row < rowIndex + count; ++row)
    {
      Node node = ((C1FlexGridBase) this).Rows[row].Node;
      if (node.Key is CUiROMGridNodeInfo key)
      {
        node.Data = (object) key.NiceName;
        node.Level = key.Level;
        key.GridRow = node.Row;
        CUiROMRoboticTargetOperationInfo key1 = node.Key as CUiROMRoboticTargetOperationInfo;
        CUiROMTargetInfo key2 = node.Key as CUiROMTargetInfo;
        CUiROMRoboticSourceOperationInfo key3 = node.Key as CUiROMRoboticSourceOperationInfo;
        CUiROMSourceInfo key4 = node.Key as CUiROMSourceInfo;
        bool flag = (key.State & CUiROMDataUtils.CellState.Selected) == CUiROMDataUtils.CellState.Selected;
        if (updateStyle)
        {
          foreach (Column col in (IEnumerable) ((C1FlexGridBase) this).Cols)
          {
            if (((RowCol) col).Index == ((C1FlexGridBase) this).Tree.Column)
            {
              if (flag)
                this.SetSelectTreeNodeStyle(row);
              else
                this.SetNormalTreeNodeStyle(row);
            }
            else if (!this.m_entireRowSelection)
              this.SetCustomCellStyle(row, ((RowCol) col).Index, CUiROMDataUtils.CellStyleCategory.Normal);
          }
        }
        if (key1 != null)
        {
          node.Image = this.GetIcon(key.NodeObject, false);
          if (((C1FlexGridBase) this).Cols.Contains("Match"))
          {
            string str = "";
            ITxObject matchedOperation = key1.MatchedOperation;
            if (matchedOperation != null)
              str = matchedOperation.Name;
            ((C1FlexGridBase) this).SetData(row, "Match", (object) str);
          }
          if (((C1FlexGridBase) this).Cols.Contains("Distance"))
          {
            string str = "";
            ((C1FlexGridBase) this).SetData(row, "Distance", (object) str);
          }
          if (((C1FlexGridBase) this).Cols.Contains("Merge"))
          {
            if (key1.ShouldMerge)
              this.SetMergeColumn(row, (CheckEnum) 1);
            else
              this.SetMergeColumn(row, (CheckEnum) 2);
          }
        }
        else if (key2 != null)
        {
          node.Image = this.GetIcon(key.NodeObject, !key2.BelongToTarget);
          if (((C1FlexGridBase) this).Cols.Contains("Match"))
          {
            string str = "";
            ITxObject matchedObject = key2.MatchedObject;
            if (matchedObject != null)
              str = matchedObject.Name;
            ((C1FlexGridBase) this).SetData(row, "Match", (object) str);
          }
          if (((C1FlexGridBase) this).Cols.Contains("Distance"))
            this.UpdateLinearColumns(row, 1);
          if (((C1FlexGridBase) this).Cols.Contains("Merge"))
          {
            bool shouldMerge = key2.ShouldMerge;
            if (key2.MatchedObject == null || !key2.BelongToTarget)
              this.SetMergeColumn(row, (CheckEnum) 0);
            else if (shouldMerge)
              this.SetMergeColumn(row, (CheckEnum) 1);
            else
              this.SetMergeColumn(row, (CheckEnum) 2);
          }
        }
        else if (key3 != null)
          node.Image = this.GetIcon(key.NodeObject, false);
        else if (key4 != null)
          node.Image = this.GetIcon(key4.NodeObject, false);
        if (((C1FlexGridBase) this).Cols.Contains("MergeSchema"))
        {
          if (key2 != null)
            ((C1FlexGridBase) this).SetData(row, "MergeSchema", (object) key2.MergeScheme);
          else if (key1 != null)
            ((C1FlexGridBase) this).SetData(row, "MergeSchema", (object) key1.MergeScheme);
        }
      }
      if (((C1FlexGridBase) this).Tree.Column > -1 && ((C1FlexGridBase) this).Cols[((C1FlexGridBase) this).Tree.Column].Name == "SourceOperation")
      {
        int autosizedColWidth = this.GetAutosizedColWidth(((C1FlexGridBase) this).Tree.Column);
        if (autosizedColWidth > 0)
          this.m_treeColumnAutosizedWidth = autosizedColWidth;
      }
    }
  }

  private Image GetIcon(ITxObject obj, bool isGrayed)
  {
    Image icon = (Image) null;
    if (obj != null)
      icon = !isGrayed ? TxIconManager.the().GetObjectIcon(obj) : TxIconManager.the().GetObjectGrayScaleIcon(obj);
    return icon;
  }

  private void RefreshTreeSelectionList()
  {
    this.m_treeSelection.Clear();
    for (int index = 1; index < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count; ++index)
    {
      Node node = ((C1FlexGridBase) this).Rows[index].Node;
      if (node != null && node.Key is CUiROMGridNodeInfo key && (key.State & CUiROMDataUtils.CellState.Selected) == CUiROMDataUtils.CellState.Selected)
        this.m_treeSelection.Add(index);
    }
  }

  public bool ClearTreeSelection()
  {
    bool flag = false;
    for (int index = this.m_treeSelection.Count - 1; index >= 0; --index)
    {
      if (this.SetNormalTreeNodeStyle(this.m_treeSelection[index]))
        flag = true;
    }
    return flag;
  }

  private bool SetSelectTreeNodeStyle(int row)
  {
    bool flag = false;
    if (row > 0 && row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
    {
      Node node = ((C1FlexGridBase) this).Rows[row].Node;
      if (node != null && node.Key is CUiROMGridNodeInfo key)
      {
        if (!this.m_treeSelection.Contains(row))
          this.m_treeSelection.Add(row);
        key.State |= CUiROMDataUtils.CellState.Selected;
        string customStyle = key.GetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select);
        if (customStyle != null && ((C1FlexGridBase) this).Styles.Contains(customStyle))
          ((C1FlexGridBase) this).SetCellStyle(row, ((C1FlexGridBase) this).Tree.Column, ((C1FlexGridBase) this).Styles[customStyle]);
        flag = true;
      }
      if (this.m_entireRowSelection)
      {
        foreach (Column col in (IEnumerable) ((C1FlexGridBase) this).Cols)
        {
          if (((RowCol) col).Index != ((C1FlexGridBase) this).Tree.Column)
            this.SetCustomCellStyle(row, ((RowCol) col).Index, CUiROMDataUtils.CellStyleCategory.Select);
        }
      }
    }
    return flag;
  }

  private bool SetNormalTreeNodeStyle(int row)
  {
    bool flag = false;
    if (row > 0 && row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
    {
      Node node = ((C1FlexGridBase) this).Rows[row].Node;
      if (node != null && node.Key is CUiROMGridNodeInfo key)
      {
        if (this.m_treeSelection.Contains(row))
          this.m_treeSelection.Remove(row);
        key.State = CUiROMDataUtils.CellState.Normal;
        string customStyle = key.GetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal);
        if (customStyle != null && ((C1FlexGridBase) this).Styles.Contains(customStyle))
          ((C1FlexGridBase) this).SetCellStyle(row, ((C1FlexGridBase) this).Tree.Column, ((C1FlexGridBase) this).Styles[customStyle]);
        flag = true;
      }
      if (this.m_entireRowSelection)
      {
        foreach (Column col in (IEnumerable) ((C1FlexGridBase) this).Cols)
        {
          if (((RowCol) col).Index != ((C1FlexGridBase) this).Tree.Column)
            this.SetCustomCellStyle(row, ((RowCol) col).Index, CUiROMDataUtils.CellStyleCategory.Normal);
        }
      }
    }
    return flag;
  }

  public void UpdateLinearColumns(int row, int count)
  {
    if (row <= 0 || row + count > ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      return;
    CultureInfo provider = new CultureInfo("en-US");
    string str1 = "";
    if (this.m_linearDigitsAfterDecimalPoint > 0)
      str1 = new string('0', this.m_linearDigitsAfterDecimalPoint);
    string format = !(str1 != "") ? "0" : "0." + str1;
    bool flag = ((C1FlexGridBase) this).Cols.Contains("Distance");
    for (int index = row; index < row + count; ++index)
    {
      Node node = ((C1FlexGridBase) this).Rows[index].Node;
      if (node != null && flag && node.Key is CUiROMTargetInfo key)
      {
        string str2 = "";
        if (key.NodeType != CUiROMDataUtils.NodeType.SeamOperation && key.MatchedObject != null)
        {
          double distance = key.Distance;
          double num = distance >= 0.25 ? Math.Round(distance, 2) : 0.0;
          if (this.m_linearMultiplier != 0.0)
            num /= this.m_linearMultiplier;
          if (key.MatchedObject != null)
            str2 = num.ToString(format, (IFormatProvider) provider);
        }
        ((C1FlexGridBase) this).SetData(index, "Distance", (object) str2);
      }
    }
  }

  public bool SetExternalTreeSelection(TxObjectList selectedObjectsList)
  {
    bool flag1 = false;
    this.m_treeSelectionChanged = false;
    ((ScrollableControl) this).BeginUpdate();
    if (selectedObjectsList != null && ((Collection<ITxObject>) selectedObjectsList).Count > 0)
    {
      for (int row = 1; row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count; ++row)
      {
        Node node = ((C1FlexGridBase) this).Rows[row].Node;
        if (node != null && node.Key is CUiROMGridNodeInfo key)
        {
          bool flag2 = false;
          if (key is CUiROMTargetInfo cuiRomTargetInfo && !cuiRomTargetInfo.BelongToTarget)
            flag2 = true;
          if (!flag2)
          {
            if (this.m_treeSelection.Contains(row))
            {
              if (key.NodeObject == null || key.NodeObject != null && !((Collection<ITxObject>) selectedObjectsList).Contains(key.NodeObject))
                flag1 = this.SetNormalTreeNodeStyle(row);
            }
            else if (key.NodeObject != null && ((Collection<ITxObject>) selectedObjectsList).Contains(key.NodeObject))
              flag1 = this.SetSelectTreeNodeStyle(row);
          }
          else
            flag1 = this.SetNormalTreeNodeStyle(row);
        }
      }
    }
    else
      flag1 = this.ClearTreeSelection();
    ((ScrollableControl) this).EndUpdate();
    return flag1;
  }

  private CUiROMDataUtils.ManualSelection TransformSelectionCase(
    CUiROMDataUtils.ManualSelection manualSelType,
    Keys pressedKeys,
    bool oldSelectValid)
  {
    CUiROMDataUtils.ManualSelection manualSelection = CUiROMDataUtils.ManualSelection.None;
    switch (manualSelType)
    {
      case CUiROMDataUtils.ManualSelection.None:
        manualSelection = CUiROMDataUtils.ManualSelection.None;
        break;
      case CUiROMDataUtils.ManualSelection.Single:
        manualSelection = CUiROMDataUtils.ManualSelection.Single;
        break;
      case CUiROMDataUtils.ManualSelection.Toggle:
        manualSelection = CUiROMDataUtils.ManualSelection.Toggle;
        break;
      case CUiROMDataUtils.ManualSelection.Ctrl:
        manualSelection = !oldSelectValid || (pressedKeys & Keys.Control) != Keys.Control ? CUiROMDataUtils.ManualSelection.Single : CUiROMDataUtils.ManualSelection.Ctrl;
        break;
      case CUiROMDataUtils.ManualSelection.Shift:
        manualSelection = !oldSelectValid || (pressedKeys & Keys.Shift) != Keys.Shift ? CUiROMDataUtils.ManualSelection.Single : CUiROMDataUtils.ManualSelection.Shift;
        break;
      case CUiROMDataUtils.ManualSelection.CtrlShift:
        if (oldSelectValid)
        {
          bool flag1 = (pressedKeys & Keys.Shift) == Keys.Shift;
          bool flag2 = (pressedKeys & Keys.Control) == Keys.Control;
          manualSelection = !flag1 ? (!flag2 ? CUiROMDataUtils.ManualSelection.Single : CUiROMDataUtils.ManualSelection.Ctrl) : (flag2 ? CUiROMDataUtils.ManualSelection.CtrlShift : CUiROMDataUtils.ManualSelection.Shift);
          break;
        }
        manualSelection = CUiROMDataUtils.ManualSelection.Single;
        break;
    }
    return manualSelection;
  }

  private void HandleInternalTreeSelection(int newSelectedRow, Keys pressedKeys)
  {
    this.m_treeSelectionChanged = false;
    ((ScrollableControl) this).BeginUpdate();
    if (newSelectedRow > 0 && newSelectedRow < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
    {
      int lastSelectedTreeRow = this.m_lastSelectedTreeRow;
      this.m_lastSelectedTreeRow = newSelectedRow;
      bool oldSelectValid = lastSelectedTreeRow > 0 && lastSelectedTreeRow < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count;
      switch (this.TransformSelectionCase(this.m_manualSelection, pressedKeys, oldSelectValid))
      {
        case CUiROMDataUtils.ManualSelection.Single:
          if (this.m_treeSelection.Count > 0)
          {
            if (this.m_treeSelection.Count != 1 || this.m_treeSelection[0] != newSelectedRow)
            {
              this.m_treeSelectionChanged = this.ClearTreeSelection();
              if (this.SetSelectTreeNodeStyle(newSelectedRow))
              {
                this.m_treeSelectionChanged = true;
                break;
              }
              break;
            }
            break;
          }
          this.m_treeSelectionChanged = this.SetSelectTreeNodeStyle(newSelectedRow);
          break;
        case CUiROMDataUtils.ManualSelection.Toggle:
        case CUiROMDataUtils.ManualSelection.Ctrl:
          if (!this.m_treeSelection.Contains(newSelectedRow))
          {
            this.m_treeSelectionChanged = this.SetSelectTreeNodeStyle(newSelectedRow);
            break;
          }
          this.m_treeSelectionChanged = this.SetNormalTreeNodeStyle(newSelectedRow);
          if (this.m_treeSelection.Count < 1)
          {
            this.m_lastSelectedTreeRow = -1;
            break;
          }
          break;
        case CUiROMDataUtils.ManualSelection.Shift:
          this.m_lastSelectedTreeRow = lastSelectedTreeRow;
          int num1 = this.m_lastSelectedTreeRow;
          int num2 = newSelectedRow;
          if (num2 < num1)
          {
            num1 = num2;
            num2 = this.m_lastSelectedTreeRow;
          }
          for (int row = 0; row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count; ++row)
          {
            if (this.m_treeSelection.Contains(row))
            {
              if ((row < num1 || row > num2) && this.SetNormalTreeNodeStyle(row))
                this.m_treeSelectionChanged = true;
            }
            else if (row >= num1 && row <= num2 && this.SetSelectTreeNodeStyle(row))
              this.m_treeSelectionChanged = true;
          }
          break;
        case CUiROMDataUtils.ManualSelection.CtrlShift:
          if (!this.m_treeSelection.Contains(newSelectedRow))
          {
            this.m_treeSelectionChanged = this.SetSelectTreeNodeStyle(newSelectedRow);
            break;
          }
          this.m_treeSelectionChanged = this.SetNormalTreeNodeStyle(newSelectedRow);
          if (this.m_treeSelection.Count < 1)
          {
            this.m_lastSelectedTreeRow = -1;
            break;
          }
          break;
      }
    }
    else
      this.m_treeSelectionChanged = this.ClearTreeSelection();
    ((ScrollableControl) this).EndUpdate();
    if (!this.m_treeSelectionChanged)
      return;
    this.NotifyTreeSelectionChanged();
  }

  private void NotifyTreeSelectionChanged()
  {
    if (this.TreeSelectionChanged == null)
      return;
    this.TreeSelectionChanged((object) this, new TreeSelectionChangedEventArgs());
    this.m_treeSelectionChanged = false;
  }

  private int GetAutosizedColWidth(int column)
  {
    int autosizedColWidth = -1;
    if (column >= 0 && column < ((RowColCollection) ((C1FlexGridBase) this).Cols).Count)
    {
      int width = ((C1FlexGridBase) this).Cols[column].Width;
      ((C1FlexGridBase) this).AutoSizeCol(column);
      autosizedColWidth = ((C1FlexGridBase) this).Cols[column].Width;
      ((C1FlexGridBase) this).Cols[column].Width = width;
    }
    return autosizedColWidth;
  }

  public void SetMergeColumn(int row, CheckEnum state)
  {
    int num = 4;
    if (((C1FlexGridBase) this).Cols.Contains("Merge"))
      num = ((RowCol) ((C1FlexGridBase) this).Cols["Merge"]).Index;
    ((C1FlexGridBase) this).SetCellCheck(row, num, state);
  }

  public bool IsMergeColumnChecked(int row)
  {
    int num = 4;
    if (((C1FlexGridBase) this).Cols.Contains("Merge"))
      num = ((RowCol) ((C1FlexGridBase) this).Cols["Merge"]).Index;
    return ((C1FlexGridBase) this).GetCellCheck(row, num) == 1;
  }

  private void CUiROMRoboticOperationGridWrapper_MouseDown(object sender, MouseEventArgs e)
  {
    if (e.Button != MouseButtons.Left)
      return;
    HitTestInfo hitTestInfo = ((C1FlexGridBase) this).HitTest(e.X, e.Y);
    if (((HitTestInfo) ref hitTestInfo).Column != ((C1FlexGridBase) this).Tree.Column && !this.m_entireRowSelection)
      return;
    if (((HitTestInfo) ref hitTestInfo).Row < 1 || ((HitTestInfo) ref hitTestInfo).Row >= ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
    {
      this.m_lastSelectedTreeRow = -1;
      this.m_treeSelectionChanged = this.ClearTreeSelection();
      if (!this.m_treeSelectionChanged)
        return;
      this.NotifyTreeSelectionChanged();
    }
    else
    {
      if (((HitTestInfo) ref hitTestInfo).Type == 11)
        return;
      this.HandleInternalTreeSelection(((HitTestInfo) ref hitTestInfo).Row, Control.ModifierKeys);
    }
  }

  private void CUiROMRoboticOperationGridWrapper_BeforeSelChange(object sender, RangeEventArgs e)
  {
    CellRange newRange1 = e.NewRange;
    if (!((CellRange) ref newRange1).IsValid)
      return;
    CellRange newRange2 = e.NewRange;
    this.m_lastSelectedRow = ((CellRange) ref newRange2).r2;
    CellRange newRange3 = e.NewRange;
    this.m_lastSelectedColumn = ((CellRange) ref newRange3).c2;
  }

  private void CUiROMRoboticOperationGridWrapper_OwnerDrawCell(
    object sender,
    OwnerDrawCellEventArgs e)
  {
    if (e.Row == this.m_lastSelectedRow && e.Col == this.m_lastSelectedColumn)
    {
      CellStyle cellStyle = ((C1FlexGridBase) this).GetCellStyle(e.Row, e.Col);
      if (cellStyle != null)
        e.Style = cellStyle;
    }
    if (e.Col != ((C1FlexGridBase) this).Tree.Column || e.Measuring)
      return;
    Node node = ((C1FlexGridBase) this).Rows[e.Row].Node;
    if (node == null || !(node.Key is CUiROMSourceInfo key) || !key.Matched)
      return;
    Graphics graphics = e.Graphics;
    Image image = this.m_notificationImages.Images[0];
    Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
    rect.Y = (int) (0.5 * (double) (2 * e.Bounds.Y + e.Bounds.Height - image.Height));
    Rectangle bounds;
    if (rect.Y < e.Bounds.Y)
    {
      ref Rectangle local = ref rect;
      bounds = e.Bounds;
      int y = bounds.Y;
      local.Y = y;
    }
    ref Rectangle local1 = ref rect;
    bounds = e.Bounds;
    int x1 = bounds.X;
    bounds = e.Bounds;
    int width = bounds.Width;
    int num1 = x1 + width - image.Width - 6;
    local1.X = num1;
    int num2 = this.m_treeColumnAutosizedWidth - 8;
    if (num2 < 0)
      num2 = 0;
    int x2 = rect.X;
    bounds = e.Bounds;
    int num3 = bounds.X + num2;
    if (x2 < num3)
      return;
    e.DrawCell();
    graphics.DrawImage(this.m_notificationImages.Images[0], rect);
  }

  private void CUiROMRoboticOperationGridWrapper_BeforeEdit(object sender, RowColEventArgs e)
  {
    if (!(((C1FlexGridBase) this).Cols[e.Col].Name == "Merge") || ((C1FlexGridBase) this).GetCellCheck(e.Row, e.Col) != null)
      return;
    e.Cancel = true;
  }
}
